package com.fitness.fitness.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.fitness.convertor.CourseScheduleConvertor;
import com.fitness.fitness.modal.coach.vo.CoachVO;
import com.fitness.fitness.modal.course.dto.CourseScheduleDto;
import com.fitness.fitness.modal.course.params.CoursePageQueryParams;
import com.fitness.fitness.modal.course.params.CourseSchedulePageQueryParams;
import com.fitness.fitness.modal.course.vo.CourseScheduleVO;
import com.fitness.fitness.modal.course.vo.CourseVO;
import com.fitness.fitness.modal.resource.vo.VenueVO;
import com.fitness.fitness.modal.response.ServerResponseEntity;
import com.fitness.fitness.service.ICourseScheduleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/course-schedule")
public class CourseScheduleController {

    private final ICourseScheduleService courseScheduleService;
    private final CourseScheduleConvertor courseScheduleConvertor;

    @GetMapping("/queryAvailableCoach")
    public ServerResponseEntity<List<CoachVO>> queryAvailableCoach(@RequestParam("startTime") String startTime,
                                                                   @RequestParam("endTime") String endTime) {
        List<CoachVO> result = courseScheduleService.queryAvailableCoach(startTime,endTime);
        return ServerResponseEntity.success(result);
    }

    @GetMapping("/queryAvailableVenue")
    public ServerResponseEntity<List<VenueVO>> queryAvailableVenue(@RequestParam("startTime") String startTime,
                                                                   @RequestParam("endTime") String endTime) {
        List<VenueVO> result = courseScheduleService.queryAvailableVenue(startTime,endTime);
        return ServerResponseEntity.success(result);
    }

    @PostMapping("/save")
    public ServerResponseEntity<Boolean> save(@RequestBody CourseScheduleDto courseScheduleDto) {
        boolean result = courseScheduleService.save(courseScheduleDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10001","添加课程失败");
    }

    /**
     * 修改
     * @param courseScheduleDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<Boolean> update(@RequestBody CourseScheduleDto courseScheduleDto) {
        boolean result = courseScheduleService.updateById(courseScheduleConvertor.dto2Entity(courseScheduleDto));

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10002","更新课程信息失败");
    }

    @GetMapping("/getCourseScheduleForm")
    public ServerResponseEntity<CourseScheduleVO>  getCourseScheduleByCourseId(Long courseId) {
        CourseScheduleVO result = courseScheduleService.getCourseScheduleByCourseId(courseId);
        return ServerResponseEntity.success(result);
    }

    @GetMapping("/getCourseSchedulesForm")
    public ServerResponseEntity<List<CourseScheduleVO>>  getCourseSchedulesByCourseId(Long courseId) {
        List<CourseScheduleVO> result = courseScheduleService.getCourseSchedulesByCourseId(courseId);
        return ServerResponseEntity.success(result);
    }

    @GetMapping("/getCourseScheduleById")
    public ServerResponseEntity<CourseScheduleVO>  getCourseScheduleById(Long id) {
        CourseScheduleVO result = courseScheduleService.getCourseScheduleById(id);
        return ServerResponseEntity.success(result);
    }

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<CourseScheduleVO>> page(CourseSchedulePageQueryParams queryParams) {
        Page<CourseScheduleVO> result = courseScheduleService.getCourseSchedulePage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(@RequestBody Long id) {
        boolean result = courseScheduleService.deleteCourseSchedule(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("CP10003","删除课程计划信息失败");
    }
}
